﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace VTV.TaskManagementSystem.BusinessLogic
{
    public class Role
    {
        public string ErrorMessage { get; set; }

        private DataAccess.Role _dataAccess;

        public bool AddNew(Entities.Role source)
        {
            try
            {
                if (!ValidationEntity(source))
                {
                    return false;
                }

                _dataAccess = new DataAccess.Role();
                var result = _dataAccess.AddNew(source);
                this.ErrorMessage = (!result) ? _dataAccess.ErrorMessage : null;
                return result;
            }
            catch (Exception e)
            {
                ErrorMessage = e.Message;

                return false;
            }
        }

        public bool Delete(Entities.Role source)
        {
            try
            {
                if (!ValidationEntity(source))
                {
                    return false;
                }

                _dataAccess = new DataAccess.Role();
                var result = _dataAccess.Delete(source);
                this.ErrorMessage = (!result) ? _dataAccess.ErrorMessage : null;
                return result;
            }
            catch (Exception e)
            {
                ErrorMessage = e.Message;

                return false;
            }
        }

        public bool Update(Entities.Role source)
        {
            try
            {
                if (!ValidationEntity(source))
                {
                    return false;
                }

                _dataAccess = new DataAccess.Role();
                var result = _dataAccess.Update(source);
                this.ErrorMessage = (!result) ? _dataAccess.ErrorMessage : null;
                return result;
            }
            catch (Exception e)
            {
                ErrorMessage = e.Message;

                return false;
            }
        }

        public int? GetCount()
        {
            try
            {
                _dataAccess = new DataAccess.Role();
                var result = _dataAccess.GetCount();
                this.ErrorMessage = result == null ? _dataAccess.ErrorMessage : string.Empty;
                return result;
            }
            catch (Exception e)
            {
                ErrorMessage = e.Message;

                return null;
            }
        }

        public IEnumerable<Entities.Role> GetAll(int start, int limit)
        {
            try
            {
                _dataAccess = new DataAccess.Role();
                var result = _dataAccess.GetAll(start, limit);
                this.ErrorMessage = result == null ? _dataAccess.ErrorMessage : string.Empty;
                return result;
            }
            catch (Exception e)
            {
                ErrorMessage = e.Message;

                return null;
            }
        }

        public Entities.Role GetRole(int roleID)
        {
            try
            {
                _dataAccess = new DataAccess.Role();
                var result = _dataAccess.GetRole(roleID);
                this.ErrorMessage = result == null ? _dataAccess.ErrorMessage : string.Empty;
                return result;
            }
            catch (Exception e)
            {
                ErrorMessage = e.Message;

                return null;
            }
        }

        private bool ValidationEntity(Entities.Role source)
        {
            try
            {
                if ((source.Name != null && !String.IsNullOrEmpty(source.Name.Trim()) && source.Name.Trim().Length > 200) ||
                    (source.Descriptions != null && !String.IsNullOrEmpty(source.Descriptions.Trim()) && source.Descriptions.Trim().Length > 200))
                {
                    ErrorMessage = Common.Constant.ValidationErrorMessage.OVERLOAD_LENGTH_OF_OBJECT;

                    return false;
                }

                return true;
            }
            catch (Exception e)
            {
                ErrorMessage = Common.Constant.ValidationErrorMessage.INVALID_VALUE + String.Format(" System message: {0}", e.Message);

                return false;
            }
        }
    }
}
